package com.example.demo2.service;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo2.common.Result;
import com.example.demo2.entity.Order;
import com.example.demo2.mapper.OrderMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ShopPlaceService shopPlaceService;
    @Autowired
    private QuestionService questionService;


    public Result getData() {
        ArrayList<Object> list = new ArrayList<>();
        ArrayList<Object> list2 = new ArrayList<>();
        ArrayList<Object> list3 = new ArrayList<>();
        List<HashMap<String, Object>> dateTO = orderMapper.getDateVO();
        dateTO.forEach(item -> {
            list.add(item.get("DATE(create_time)"));
            list2.add(item.get("SUM(total_money)"));
            list3.add(item.get("COUNT(1)"));
        });

        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("x", list);
        map2.put("y1", list2);
        map2.put("y2", list3);

        List<Order> orderList = orderMapper.selectList(null);
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        for (Order order : orderList) {
            Date createTime = Date.valueOf(order.getCreateTime().toLocalDate());
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter) {
                case Q1:
                    q1++;
                    break;
                case Q2:
                    q2++;
                    break;
                case Q3:
                    q3++;
                    break;
                case Q4:
                    q4++;
                    break;
                default:
                    break;
            }
        }
        map2.put("pie", CollUtil.newArrayList(q1, q2, q3, q4));

        int orderSize = orderMapper.selectList(null).size();
        int customerSize = customerService.list().size();
        int shopPlaceSize = shopPlaceService.list().size();
        int questionSize = questionService.list().size();
        map2.put("orderSize", orderSize);
        map2.put("customerSize", customerSize);
        map2.put("shopPlaceSize", shopPlaceSize);
        map2.put("questionSize", questionSize);
        return Result.success(map2);
    }
}